<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input class="w500" v-model="form.name" placeholder="例如：测试"></el-input>
      </el-form-item>
      <el-form-item label="英文名称" prop="en_name">
        <el-input class="w500" v-model="form.en_name" placeholder="例如：ceshi"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-radio-group v-model="form.attr_type">
          <el-radio label="1">文本</el-radio>
          <!--<el-radio label="2">多行文本</el-radio>-->
          <el-radio label="3">单选</el-radio>
          <el-radio label="4">多选</el-radio>
          <el-radio label="5">下拉</el-radio>
          <!--<el-radio label="6">文件</el-radio>-->
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认值" >
        <el-input class="w500" v-model="form.default_value" ></el-input>
      </el-form-item>
      <el-form-item label="属性集" >
        <el-input class="w500" type="textarea" v-model="form.attr_values" ></el-input>
      </el-form-item>
      <el-form-item label="参与筛选">
        <el-radio-group v-model="form.is_filter">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="位置">
        <el-radio-group v-model="form.show_position">
          <el-radio label="main">主</el-radio>
          <el-radio label="secondary">次</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="关联字段">
        <el-radio-group v-model="form.search_flag">
          <el-radio label="number">库存</el-radio>
          <el-radio label="none">无</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="order">
        <el-input class="w500" v-model="form.order" type="number"></el-input>
      </el-form-item>
      <el-form-item label="是否展示">
        <el-radio-group v-model="form.is_show">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        form: {
          name: '',
          en_name: '',
          attr_type: '1',
          default_value: '',
          attr_values: '',
          is_filter: '1',
          show_position: 'main',
          search_flag: 'none',
          order: 1,
          is_show: '1'
        },
        rules: {
          name: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ],
          en_name: [
            {required: true, message: '英文名称', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('goodsattr/goodsAttrAdd', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/goods/goodsattr')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
